/**
 * Description:
 * Class Name:Server
 * Date:2022/5/24 10:33
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第17章_网络编程._17_3_基于TCP协议的网络编程._17_3_5_记录用户信息;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static final int SERVER_PORT = 30000;
    // 使用 CrazyItMap 对象来保存每个客户名字和对应输出流之间的对应关系
    public static CrazyItMap<String, PrintStream> clients = new CrazyItMap<>();

    public void init(){
        try (var ss = new ServerSocket(SERVER_PORT);){
            while (true){
                Socket socket = ss.accept();
                new ServerThread(socket).start();
            }
        }
        // 如果抛出异常
        catch (IOException e){
            System.out.println("服务器启动失败，是否端口：" + SERVER_PORT + " 已被占用？");
        }
    }

    public static void main(String[] args) {
        var server = new Server();
        server.init();
    }
}
